package net.zjitc.queue;

import com.sun.org.apache.regexp.internal.RE;
import net.zjitc.list.MyList;

public class ArraysQueue<T> implements MyQueue<T> {

    private int MaxSize = 10;
    private Object[] arr  = new Object[MaxSize];
    private int front = 0;
    private int rear = 0;

    //长度由参数决定
    public ArraysQueue(int maxSize) {
        MaxSize = maxSize;
    }

    //默认长度
    public ArraysQueue() {
    }

    /**
     * 入队
     * @param x
     */
    @Override
    public boolean add(T x) {
         arr[rear] = x;
         rear = (rear+1)%MaxSize;
         return true;
    }

    /**
     * 出队
     */
    @Override
    public T poll() {
        T var = (T) arr[front];
        arr[front] = null;
        front = (front+1)%MaxSize;
        return var;
    }

    /**
     * 查看队头
     */
    @Override
    public T peek() {
        return (T) arr[front];
    }

    /**
     * 判断是否为空
     */
    @Override
    public boolean isEmpty() {
        return rear==front?true:false;
    }

    public boolean isFull(){
        return (rear+1)%MaxSize == front?true:false;
    }

    /**
     * 有效元素
     * @return
     */
    public int getSize(){
        return (rear-front+MaxSize)%MaxSize;
    }

    //打印队列中所有的元素？？

}
